<template>
  <a-modal v-model:open="open" title="分享文件"
           @ok="handleOk"
           @cancel="close"
           @close="close">
    <div>
      <a-form>
        <div class="fileName"  v-if="selectFileList.length>0">
          {{ selectFileList[0].name + `等${selectFileList.length}个文件` }}
        </div>
        <a-form-item label="过期时间">
          <a-radio-group v-model:value="from.expireDays" button-style="solid">
            <a-radio-button :value="1">1天</a-radio-button>
            <a-radio-button :value="7">7天</a-radio-button>
            <a-radio-button :value="30">30天</a-radio-button>
            <a-radio-button :value="-1">永久</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="提取码">
            <div class="flex alignCenter">
              <a-input v-model:value="from.code"
                       maxlength="5"
                       placeholder="请输入提取码,为空则不需要提取码"/>
              <a-button style="margin-left: 5px" type="primary"
              @click="generateCode"
              >随机生成</a-button>
            </div>
        </a-form-item>
      </a-form>

    </div>
  </a-modal>
</template>
<script setup lang="ts">
import {computed, ref, watch} from "vue";
import {generateShareUrl,generateShareCode} from '@/api/disk/share.ts'
import {FilePrimary, ShareFrom} from "@/types/global.ts";
import {message} from "ant-design-vue";
const props = defineProps({
  modelValue:{
    type:Boolean,
    default:false
  },
  selectFileList:{
    type:Array,
    default: []
  }
})
const emits = defineEmits(['update:modelValue','confirm']);
const open = computed(()=>props.modelValue)
const selectFileList = computed(()=>props.selectFileList as FilePrimary[])
const generateForm = ():ShareFrom=>{
  return{
    expireDays: 1,
    code: '',
    fileIds:'',
    name:''
  } as ShareFrom
}
const from = ref(generateForm());


const handleOk = ()=>{
  from.value.fileIds =  selectFileList.value.map(item=>item.id);
  from.value.name =  `${selectFileList.value[0].name+"等"+selectFileList.value.length}个文件`;
  if(selectFileList.value.length===1){
    from.value.name =  `${selectFileList.value[0].name}`;
  }
  generateShareUrl(from.value).then((res:any)=>{
    message.success('生成成功')
    emits("confirm",res.result)
  })
}
const generateCode = ()=>{
  generateShareCode().then((res:any)=>{
    console.log(res.result)
    from.value.code = res.result;
    console.log( from.value)
  })
}
const close = () => {
  emits("update:modelValue",false)
}


</script>
<style scoped lang="scss">
.fileName{
  font-size: 24px;
  font-weight: bold;
  margin: 20px 0;
}
</style>
